package pkg

import (
	"fmt"
	"github.com/dgrijalva/jwt-go"
)

var Exp = 60 * 60 * 24
var Se = "15"

func GetJwtToken(payload string) (string, error) {
	claims := make(jwt.MapClaims)
	claims["exp"] = Exp
	claims["iat"] = Se
	claims["payload"] = payload
	token := jwt.New(jwt.SigningMethodHS256)
	token.Claims = claims
	return token.SignedString([]byte(Se))
}
func Authjwt(tokenstring string) (int64, error) {
	token, err := jwt.Parse(tokenstring, func(token *jwt.Token) (interface{}, error) {
		// Don't forget to validate the alg is what you expect:
		if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
			return nil, fmt.Errorf("Unexpected signing method: %v", token.Header["alg"])
		}

		// hmacSampleSecret is a []byte containing your secret, e.g. []byte("my_secret_key")
		return []byte(Se), nil
	})

	if claims, ok := token.Claims.(jwt.MapClaims); ok && token.Valid {
		return claims["payload"].(int64), err
	} else {
		fmt.Println(err)
	}
	return 0, err
}
